<?php

/**
 * Created by PhpStorm.
 * User: TC
 * Date: 2016/3/28
 * Time: 15:15
 * 加盟控制器
 */
require_once dirname(__FILE__) . '/../db/medoo.php';
require_once dirname(__FILE__) . '/../model/JoinModel.php';

class JoinController
{
  /*
   * 添加加盟信息
   */
  public static function AddJoin($tel, $name, $province, $city, $ext)
  {
    if (!$tel) return false;
    if (!$name) return false;
    if (!$province) return false;
    if (!$city) return false;
    $database = new medoo();
    $lastId = $database->insert(JoinModel::TABLE_NAME, [
      JoinModel::TEL_FIELD => $tel,
      JoinModel::NAME_FIELD => $name,
      JoinModel::PROVINCE_FIELD => $province,
      JoinModel::CITY_FIELD => $city,
      JoinModel::EXT_FIELD => $ext,
      JoinModel::FLAG_FIELD => 0,
      JoinModel::CREATEAT_FIELD => date('Y-m-d H:i:s')
    ]);
    return $lastId > 0 ? true : false;
  }

  /*
   * 获取加盟列表
   */
  public static function JoinList($page = 1, $count = 20)
  {
    $database = new medoo();
    $index = ($page - 1) * $count;
    $joinList = $database->select(JoinModel::TABLE_NAME, '*', [
      'ORDER' => JoinModel::CREATEAT_FIELD . ' DESC',
      'LIMIT' => [$index, $count]
    ]);
    return $joinList;
  }

  /*
   * 检查该手机号是否已经提交加盟
   */
  public static function CheckTel($tel)
  {
    if (!$tel) return false;
    $database = new medoo();
    $result = $database->select(JoinModel::TABLE_NAME, '*', [JoinModel::TEL_FIELD => $tel]);
    return (count($result) > 0) ? true : false;
  }
}